package stark.app.function.netusage;

import android.widget.ProgressBar;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ConvertUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.netusage.lib.AppNetUsageInfo;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import stark.app.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;

/**
 * Author：daizhongbin on 2022/8/10
 * Email：1027154338@qq.com
 * Desc:
 */
public class NetUsageAppAdapter extends StkProviderMultiAdapter<AppNetUsageInfo> {

    //是否显示移动流量的数据 true:是  false：显示WiFi的
    private boolean showMobie = true;

    public NetUsageAppAdapter() {
        super(1);
        addItemProvider(new MyItemProvider());
    }

    public void setShowMobie(boolean showMobie) {
        this.showMobie = showMobie;
        List<AppNetUsageInfo> infos = getData();
        if (infos == null || infos.size() == 0) return;

        List<AppNetUsageInfo> newInfos = new ArrayList<>(infos);
        Collections.sort(newInfos, new Comparator<AppNetUsageInfo>() {
            @Override
            public int compare(AppNetUsageInfo app1, AppNetUsageInfo app2) {
                long app1Data = showMobie ? app1.getMobileTotalBytes() : app1.getWifiTotalBytes();
                long app2Data = showMobie ? app2.getMobileTotalBytes() : app2.getWifiTotalBytes();
                //从大到小排列
                return (int) (app2Data - app1Data);
            }
        });
        setNewInstance(newInfos);
    }

    public boolean isShowMobie() {
        return showMobie;
    }

    private int getPercent(AppNetUsageInfo info) {
        int percent = 0;
        long dataBytes = showMobie ? info.getMobileTotalBytes() : info.getWifiTotalBytes();
        long total = showMobie ? getMobileTotal() : getWifiTotal();
        percent = (int) (dataBytes * 1f / total * 100);
        return percent;
    }

    private long getMobileTotal() {
        long total = 0;
        List<AppNetUsageInfo> infos = getData();
        if (infos == null) {
            return total;
        }
        for (AppNetUsageInfo info : infos) {
            total += info.getMobileTotalBytes();
        }
        return total;
    }

    private long getWifiTotal() {
        long total = 0;
        List<AppNetUsageInfo> infos = getData();
        if (infos == null) {
            return total;
        }
        for (AppNetUsageInfo info : infos) {
            total += info.getWifiTotalBytes();
        }
        return total;
    }

    private class MyItemProvider extends BaseItemProvider<AppNetUsageInfo> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_app_net_usage;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, AppNetUsageInfo appInfo) {
            holder.setImageDrawable(R.id.ivIcon, AppUtils.getAppIcon(appInfo.pckName));
            holder.setText(R.id.tvAppName, AppUtils.getAppName(appInfo.pckName));

            long dataBytes = showMobie ? appInfo.getMobileTotalBytes() : appInfo.getWifiTotalBytes();
            holder.setText(R.id.tvData, ConvertUtils.byte2FitMemorySize(dataBytes, 1));
            ProgressBar pbPercent = holder.getView(R.id.pbPercent);
            pbPercent.setProgress(getPercent(appInfo));
        }
    }

}
